package com.vhall.component.service.room.event.handler.record;

import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.service.room.concant.RoomConstant;
import com.vhall.component.service.room.event.handler.AbstractEventHandler;
import com.vhall.component.service.room.event.handler.EventHandlerType;
import com.vhall.component.service.room.event.handler.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.vhall.component.service.room.event.EventConstant.MEDIA_PACKAGE_COMPLETE;


/**
 * 媒体打包完成 处理器
 *
 * @author jingwen.li
 * @date 2023/2/16
 */
@Slf4j
@Component
@EventHandlerType(MEDIA_PACKAGE_COMPLETE)
public class MediaPackageCompleteEventHandler extends AbstractEventHandler {
    @Autowired
    private RedirectUtil redirectUtil;
    @Value("${old.callback.record.url}")
    private String callbackUrl;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void handle(Map<String, Object> callBack) {
//        redirectUtil.redirect(callbackUrl, callBack);
        log.info("收到媒体打包完成事件：{}, 事件详情：{}", MEDIA_PACKAGE_COMPLETE, callBack);
        String vodId = (String) callBack.get(RoomConstant.VOD_ID);
        String quality = (String) callBack.get("quality");
        String downloadUrl = (String) callBack.get("download_url");
        String key = StringUtils.isNotBlank(quality)
                ? RedisKey.RECORD_DOWN_QA_URL + vodId + ":" + quality
                : RedisKey.RECORD_DOWN_URL + vodId;
        if (StringUtils.isNotBlank(downloadUrl)) {
            redisTemplate.opsForValue().set(key, downloadUrl, 6, TimeUnit.DAYS);
        }
    }
}
